import Axios from 'axios'
import { ElMessage } from 'element-plus'

import { BASE_URL, TIME_OUT } from "./config"

const request = Axios.create({
    baseURL: BASE_URL,
    timeout: TIME_OUT
})

request.interceptors.request.use((config) => {
    return config
}, err => {
    return Promise.reject(err)
})

request.interceptors.response.use((response) => {
    if (response) {
        return response.data
    }
}, err => {
    if (err.response && err.response.data) {
        const code = err.response.status
        const msg = err.response.data.message
        ElMessage.error(`Code: ${code}, Message: ${msg}`)
    } else {
        ElMessage.error(`${err}`)
    }
    return Promise.reject(err)
})


export default request
